﻿#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

//链表结点
struct LinkNode
{
	//数据域
	void* data;
	//结点域
	struct LinkNode* next;
};

//链表结构体，管理链表
struct LList
{
	//头结点
	struct LinkNode pHeader;
	//大小
	int m_size;
};

typedef void* LinkList; //定义一个数据类型为 void*  防止用户操作链表结构体


//初始化链表
LinkList init_LinkList()  //返回的链表为LinkList = struct LList*
{
	struct LList* myList = malloc(sizeof(struct LList));

	if (myList == NULL)
	{
		return NULL;
	}

	myList->pHeader.data = NULL;
	myList->pHeader.next = NULL;
	myList->m_size = 0;

	return myList;
}

//插入数据
void insert_LinkList(LinkList list, int pos, void* data)
{
	if (list == NULL)
	{
		return;
	}
	if (data == NULL)
	{
		return;
	}
	//将传入的链表转换成struct LList *数据类型
	struct LList* myList = list;
	if (pos < 0 || pos >myList->m_size)
	{
		//转换为尾插
		pos = myList->m_size;
	}

	//找到头结点所在位置
	struct LinkNode* pCurrent = &myList->pHeader;

	for (int i = 0; i < pos; i++)
	{
		pCurrent = pCurrent->next;
	}
	//创建新的结点
	struct LinkNode* newNode = malloc(sizeof(struct LinkNode));
	newNode->data = data;
	newNode->next = NULL;

	//插入新的结点
	newNode->next = pCurrent->next;
	pCurrent->next = newNode;

	//链表数量加1
	myList->m_size++;

}

//遍历链表
void foreach_LinkList(LinkList list, void(*myForeach)(void*))
{
	if (list == NULL)
	{
		return;
	}

	struct LList* mylist = list;

	struct LinkNode* pCurrent = mylist->pHeader.next;

	for (int i = 0; i < mylist->m_size; i++)
	{
		myForeach(pCurrent->data);
		pCurrent = pCurrent->next;
	}

}


//删除链表元素--通过位置
void removeByPos_LinkList(LinkList list, int pos)
{
	if (list == NULL)
	{
		return;
	}

	struct LList* mylist = list;

	if (pos < 0 || pos > mylist->m_size - 1)
	{
		return;
	}

	//创建当前节点
	struct LinkNode* pCurrent = &mylist->pHeader;
	for (int i = 0; i < pos; i++)
	{
		pCurrent = pCurrent->next;
	}

	//找到需要删除的结点
	struct LinkNode* pDel = pCurrent->next;

	//将当前节点指向需要删除结点的后一个结点
	pCurrent->next = pDel->next;
	
	free(pDel);
	pDel = NULL;

	//更新链表长度
	mylist->m_size--;
}

//通过值删除元素
void removeByValue_LinkList(LinkList list, void* data, int(*myCompare)(void*, void*))
{
	if (list == NULL)
	{
		return;
	}
	if (data == NULL)
	{
		return;
	}

	struct LList* mylist = list;
	//ŽŽœšÁœžöžšÖúÖžÕë
	struct LinkNode* pPrev = &mylist->pHeader;
	struct LinkNode* pCurrent = pPrev->next;

	for (int i = 0; i < mylist->m_size; i++)
	{
		//pCurrent->data  data œ«ÁœžöÖžÕë±ÈœÏÀûÓÃ»Øµ÷ œ»žøÓÃ»§
		if (myCompare(pCurrent->data, data))
		{
			pPrev->next = pCurrent->next;

			free(pCurrent);
			pCurrent = NULL;

			mylist->m_size--;
			break;
		}

		//žšÖúÖžÕëºóÒÆ
		pPrev = pCurrent;
		pCurrent = pCurrent->next;
	}
}

//清空链表
void clear_LinkList(LinkList list)
{
	if (list == NULL)
	{
		return;
	}

	struct LList* mylist = list;

	struct LinkNode* pCurrent = mylist->pHeader.next;

	for (int i = 0; i < mylist->m_size; i++)
	{
		struct LinkNode* pNext = pCurrent->next;

		free(pCurrent);

		pCurrent = pNext;
	}

	mylist->pHeader.next = NULL;
	mylist->m_size = 0;

}

//返回链表大小
int  size_LinkList(LinkList list)
{
	if (list == NULL)
	{
		return -1;
	}

	struct LList* mylist = list;

	return mylist->m_size;
}

//销毁链表
void destroy_Linklist(LinkList list)
{
	if (list == NULL)
	{
		return;
	}

	//清空链表
	clear_LinkList(list);

	free(list);

	list = NULL;

}




//²âÊÔ 
struct Person
{
	char name[64];
	int age;
};

void myPrintPerson(void* data)
{
	struct Person* p = data;
	printf("ÐÕÃû£º%s  ÄêÁä£º%d\n", p->name, p->age);
}

int myComparePerson(void* data1, void* data2)
{
	struct Person* p1 = data1;
	struct Person* p2 = data2;

	return strcmp(p1->name, p2->name) == 0 && p1->age == p2->age;
}

void test01()
{
	//×Œ±žÊýŸÝ
	struct Person p1 = { "ÑÇÉª", 18 };
	struct Person p2 = { "æ§Œº", 20 };
	struct Person p3 = { "°²ç÷À­", 19 };
	struct Person p4 = { "¿­", 21 };
	struct Person p5 = { "ËïÎò¿Õ", 999 };
	struct Person p6 = { "Àî°×", 999 };

	//³õÊŒ»¯ÁŽ±í
	LinkList mylist = init_LinkList();

	printf("ÁŽ±í³€¶ÈÎª£º%d\n", size_LinkList(mylist));

	//²åÈëÊýŸÝ
	insert_LinkList(mylist, 0, &p1);
	insert_LinkList(mylist, 0, &p2);
	insert_LinkList(mylist, -1, &p3);
	insert_LinkList(mylist, 0, &p4);
	insert_LinkList(mylist, 1, &p5);
	insert_LinkList(mylist, 0, &p6);



	// Àî°× ¿­ ËïÎò¿Õ æ§Œº ÑÇÉª °²ç÷À­

	//±éÀú
	foreach_LinkList(mylist, myPrintPerson);

	printf("ÁŽ±í³€¶ÈÎª£º%d\n", size_LinkList(mylist));

	//²âÊÔÉŸ³ý 
	removeByPos_LinkList(mylist, 4);

	printf("------------------\n");

	foreach_LinkList(mylist, myPrintPerson);
	printf("ÁŽ±í³€¶ÈÎª£º%d\n", size_LinkList(mylist));

	struct Person p = { "ËïÎò¿Õ", 999 };
	removeByValue_LinkList(mylist, &p, myComparePerson);

	printf("------------------\n");

	foreach_LinkList(mylist, myPrintPerson);
	printf("ÁŽ±í³€¶ÈÎª£º%d\n", size_LinkList(mylist));

	//²âÊÔÇå¿Õ
	clear_LinkList(mylist);

	//·µ»ØÁŽ±í³€¶È
	printf("ÁŽ±í³€¶ÈÎª£º%d\n", size_LinkList(mylist));

	//Ïú»Ù
	destroy_Linklist(mylist);
	mylist = NULL;
}

int main() {

	test01();

	system("pause");
	return EXIT_SUCCESS;
}
